home *** CD-ROM | disk | FTP | other *** search
/ Hackers Handbook - Millenium Edition / Hackers Handbook.iso / files / c_scripts / sbounce.perl < prev    next >
Encoding:
Text File  |  1999-04-11  |  6.3 KB  |  278 lines

  1. #!/usr/bin/perl -w
  2. #
  3. # Here is a nice little irc bounce prog I made. The command line is:
  4. #  sbounce irc.server.com ircport bounceport
  5. # For example "sbounce irc.blackened.com 6667 5555"
  6. # There are two passwords you can set:
  7. # use -pass password to set a password to keep out the undesirables.
  8. # You can then type on most clients "/server bouce.host.com port password"
  9. # use -admin password to set up administator access.  Then you can change
  10. # settings remotely.  Use -maxcli # to limit the number of simaltaneous users.
  11. # use the -logoff options to turn of logging.  Otherwise, info is stored in
  12. # bounce.log in the same dir as the script.
  13. # [SB]Tikiman
  14.  
  15. use Socket;
  16.  
  17. $remote = $ARGV[0];
  18. $port = $ARGV[1];
  19. $bport = $ARGV[2];
  20. $log = 'True';
  21.  
  22. foreach (reverse @ARGV) {
  23.  (/^-max/i) && ($maxcli = $lastval);
  24.  (/^-pass/i) && ($password = $lastval);
  25.  (/^-admin/i) && ($masterpass = $lastval);
  26.  (/^-logoff/i) && ($log = ''); 
  27.  $lastval = $_;
  28. }
  29.  
  30. ($remote) && ($port) && ($bport) || ( comhelp() ) ; 
  31.  
  32. sub sockclose {
  33.  close(SOCK);
  34. }
  35.  
  36. sub cliclose {
  37.  close(GOTCLI);
  38. }
  39.  
  40. sub clidec {
  41.  --$connections;
  42. }
  43.  
  44. sub timeout {
  45.  print GOTCLI "ERROR :Closing Link: Ping Timeout (No soup for you!)\n";
  46.  close(GOTCLI);
  47. }
  48.  
  49.  
  50. sub logclose {
  51.  ($$ == $bigdaddy) && bnlog("Bounce Server Terminated at " . localtime() . ".\n") ;
  52.  exit;
  53. }
  54.  
  55. $SIG{'ALRM'} = 'timeout';
  56. $SIG{'TERM'} = 'logclose';
  57. $SIG{'PIPE'} = 'clidec';
  58. $SIG{'USR1'} = 'sockclose';
  59. $SIG{'TRAP'} = 'cliclose';
  60.  
  61. $bigfork = fork();
  62. if ($bigfork) {
  63.  print "Listening on $bport - process $bigfork\n";
  64.  defined($maxcli) && print "Max Clients: $maxcli\n";
  65.  defined($password) && print "Access password is $password\n"; 
  66.  defined($masterpass) && print "Master password is $masterpass\n";
  67.  exit;
  68. }
  69.  
  70. $bigdaddy = $$;
  71. $connections = 0;
  72.  
  73. $proto = getprotobyname('tcp');
  74. socket(WAITCLI, PF_INET, SOCK_STREAM, $proto);
  75. setsockopt(WAITCLI, SOL_SOCKET, SO_REUSEADDR, pack("l", 1));
  76. bind(WAITCLI, sockaddr_in($bport, INADDR_ANY) ) || die "Cannot listen on $bport\n"; 
  77. listen(WAITCLI,20);
  78. bnlog("Bounce server to $remote:$port activated at " . localtime() . ".\n");
  79. while ($ipaddr = accept(GOTCLI,WAITCLI)) {
  80.  ($name,$iaddrr) = sockaddr_in($ipaddr);
  81.  ($name) = gethostbyaddr($iaddrr,AF_INET);
  82.  select(GOTCLI);
  83.  $| = 1;
  84.  select(STDOUT);
  85.  alarm 5;
  86.  if ($_ = <GOTCLI>) {
  87.  
  88.  }
  89.  else {
  90.   bnlog("[$name] Timeout at " . localtime() . "\n");
  91.   next;
  92.  }
  93.  alarm 0;
  94.  admincheck() && next;
  95.  maxclicheck() && next;
  96.  passcheck() && next;
  97.  ++$connections;
  98.  conser();
  99.  $spawn = fork();
  100.  if ($spawn) {
  101.   next;
  102.  }
  103.  $daddy = $$;
  104.  bnlog("[$name] Bounce login at " . localtime() . "\n");
  105.  $lilkid = fork();
  106.  if ($lilkid) {
  107.   while (<SOCK>) {
  108.    print GOTCLI $_;
  109.   } 
  110.  kill('TRAP',$lilkid);
  111.  exit;
  112.  }
  113.  else {
  114.   while (<GOTCLI>) {
  115.    if (/^die/i) {
  116.     print GOTCLI "Bounce server terminated, current connections maintained.\n";
  117.     kill("TERM",$bigdaddy);
  118.     next;
  119.    }
  120.    print SOCK $_;
  121.   }
  122.   bnlog("[$name] Disconnected at " . localtime() . "\n");
  123.   kill("PIPE",$bigdaddy);
  124.   kill("USR1",$daddy);
  125.  } 
  126.  exit;
  127. }
  128.  
  129. sub conser {
  130.  $iaddr = inet_aton($remote);
  131.  $paddr = sockaddr_in($port, $iaddr);
  132.  $proto = getprotobyname('tcp');
  133.  close(SOCK);
  134.  socket(SOCK, PF_INET, SOCK_STREAM, $proto)  || die "socket: $!";
  135.  unless(connect(SOCK, $paddr)) {
  136.   print GOTCLI "Cannot connect to Irc Server\n";
  137.   return();
  138.  }
  139.  select(SOCK);
  140.  $| = 1;
  141.  select('stdout');
  142.  print SOCK;
  143.  print GOTCLI "*** NOTICE AUTH :Welcome to sbounce - Client number $connections\n";
  144.  print GOTCLI "*** NOTICE AUTH :Now redirectiong to $remote on $port\n";
  145. }
  146.  
  147. sub comhelp {
  148.  print STDOUT <<helpblock;
  149. Usage:
  150.  sbounce irc.server.com ircport bounceport
  151.   -max \# (max connections)
  152.   -pass password (set access password)
  153.   -admin password (set administrator password)
  154.   -logoff (disables logging)
  155. helpblock
  156.  exit;
  157. }
  158.  
  159. sub bnlog {
  160.    ($line) = @_;
  161.    ($log eq 'True') || return();
  162.    open(LOG,">>bounce.log");
  163.    chomp($line);
  164.    print LOG $line . "\n";
  165.    close(LOG);
  166. }
  167.  
  168. sub admincheck {
  169.  if ( defined($masterpass) ) {
  170.   if (/^pass\s$masterpass\b/i ) {
  171.    bnlog("[$name] Administrator login at " . localtime() . "\n");
  172.    print GOTCLI "NOTICE AUTH :*** Welcome administrator\n";
  173.    climen();
  174.    while (defined($_ = <GOTCLI>)) {
  175.     if (/^QUIT\b/i) {
  176.       last;
  177.     }
  178.     if (/^DIE\b/i) {
  179.       print GOTCLI "Bounce terminating\n";
  180.       exit;
  181.     }
  182.     if (/^SERVER\s(\S+)/i) {
  183.      $remote = $1;
  184.      print GOTCLI "New bounce server - $remote\n";
  185.     }
  186.     if (/^PORT\s(\S+)/i) {
  187.      $port = $1;
  188.       print GOTCLI "New bounce port - $port\n";
  189.     }
  190.     if (/^HELP\b/i) {
  191.       clicom();
  192.     } 
  193.     if (/^maxcli\s(.+)|^maxcli\b/i) {
  194.      $maxcli = $1;
  195.      if (defined($maxcli)) {
  196.       print GOTCLI "New client limit - $maxcli\n";
  197.      }
  198.      else {
  199.       print GOTCLI "Limit cleared -\n";
  200.      }
  201.     }
  202.  
  203.     if (/^password\s(.+)|^password\b/i) {
  204.      $password = $1;
  205.      if (defined($password)) {
  206.       print GOTCLI "New access password - $password\n";
  207.      }
  208.      else {
  209.       print GOTCLI "Password cleared -\n";
  210.      }
  211.     }
  212.  
  213.    }
  214.    close(GOTCLI);
  215.    return("Outta here!");
  216.   }
  217.  }
  218.  return();
  219. }
  220.  
  221. sub maxclicheck {
  222.  if (defined($maxcli)) {
  223.   if ($connections == $maxcli) {
  224.    bnlog("[$name] Client Max Reached at " . localtime() ."n");
  225.    print GOTCLI "ERROR :Closing Link: Maximum Bounce connections reached\n";
  226.    return("Lets go");
  227.   }
  228.  }
  229.  return();
  230. }
  231.  
  232. sub passcheck {
  233.  if ( defined($password) ) {
  234.   unless (/^pass\s$password\b/i) {
  235.    bnlog("[$name] Bad Password " . localtime() . "\n");
  236.    print GOTCLI "ERROR :Closing Link: Unauthorized access\n";
  237.    close(GOTCLI);
  238.    return("Byebye\n");
  239.   }
  240.  }
  241.  return();
  242. }
  243.  
  244. sub climen {
  245.    select(GOTCLI);
  246.    print GOTCLI <<clihelp ; 
  247. * Bounces in progress - $connections
  248. * Current Server - $remote
  249. * Current Port - $port
  250. clihelp
  251.    if (defined($maxcli)) {
  252.     print "* Client Limit - $maxcli\n";
  253.  
  254.    }
  255.    else {
  256.     print "* No client limit set\n";
  257.    }
  258.    if (defined($password)) {
  259.     print "* Access password - $password\n";
  260.    }
  261.    else {
  262.     print "* No access password set\n";
  263.    }
  264.    print "Type \"/raw help\" or \"/quote help\" for a list of commands\n";
  265.    select(STDOUT);
  266. }
  267.  
  268. sub clicom {
  269.  print GOTCLI <<help
  270. * Commands - use \"/raw commands\" or \"/quote command\" to execute
  271. * SERVER servername (Select new bounce server)
  272. * PORT port (Select new bounce server port)
  273. * MAXCLI limit (Sets new limit, \"/raw MAXCLI\" to clear
  274. * PASSWORD newpass (Sets new access password, \"/raw PASSWORD\" to allow open access
  275. help
  276.  
  277. }
  278.